package com.quotes.charts;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Date;
import javax.swing.JPanel;
import org.apache.log4j.Logger;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.time.Day;
import org.jfree.data.xy.*;
import org.jfree.ui.Layer;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.TextAnchor;
import org.jfree.chart.axis.*;
import org.jfree.chart.renderer.xy.*;
import com.quotes.datamodell.*;
import com.quotes.dates.TimeRange;

public abstract class LineChart extends JPanel 
{
	protected Logger logger = Logger.getLogger(getClass().toString());
	protected static final long serialVersionUID = 1L;
	protected Quotes quotes = null;
	protected TimeRange range = null;
	
	public LineChart() {
	}
	
	public LineChart(Quotes quotes, TimeRange range) {
		this.quotes = quotes;
		this.range = range;
		
        logger.debug("LineChart created!");
	}
	
	protected void createChart(Quotes quotes) {
		XYPlot plot = createPlot(quotes);
		JFreeChart chart = new JFreeChart(plot);
		ChartPanel panel = new ChartPanel(chart);
		this.setLayout(new BorderLayout());
        this.add(panel, BorderLayout.CENTER);
	}

	private XYPlot createPlot(Quotes product) {
        XYDataset xyDataset = createXyDataset(product);
        DateAxis domainAxis = createDateAxis();
        NumberAxis rangeAxis = createNumberAxis();
    	XYPlot plot = new XYPlot(xyDataset, domainAxis, rangeAxis, new XYLineAndShapeRenderer(true, false));
    	return manipulatePlot(plot);
	}
	
	protected XYPlot manipulatePlot(XYPlot plot) {
		return plot;
	}
	
	private NumberAxis createNumberAxis() {
		NumberAxis rangeAxis = new NumberAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
        rangeAxis.setAutoRange(true);
		return rangeAxis;
	}

	private DateAxis createDateAxis() {
		DateAxis domainAxis = new DateAxis();
        domainAxis.setAutoRange(true);
        domainAxis.setTickLabelsVisible(true);
        domainAxis.setAutoTickUnitSelection(true);
		return domainAxis;
	}
	
	protected void addHorizontalMarker(XYPlot plot, Date date, Color color) {
		final Day day = new Day(date);
        final Marker marker = new ValueMarker(day.getFirstMillisecond());
        marker.setPaint(color);
        marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
        marker.setLabelTextAnchor(TextAnchor.CENTER);
        marker.setStroke(new BasicStroke(1));
        plot.addDomainMarker(marker, Layer.BACKGROUND);
	}
	
	protected void addHorizontalMarker(XYPlot plot, TimeRange range, Color color) {
		final Day start = new Day(range.getStart());
		final Day end = new Day(range.getEnd());
		final IntervalMarker marker = new IntervalMarker(start.getFirstMillisecond(), end.getFirstMillisecond());
        marker.setPaint(color);
        marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
        marker.setLabelTextAnchor(TextAnchor.CENTER);
        marker.setStroke(new BasicStroke(1));
		plot.addDomainMarker(marker, Layer.BACKGROUND);
	}
	
	protected void addVerticalMarker(XYPlot plot, double value, Color color) {
        final Marker marker = new ValueMarker(value);
        marker.setPaint(color);
        plot.addRangeMarker(marker, Layer.BACKGROUND);
    }
	    
	protected abstract XYDataset createXyDataset(Quotes product);
}
